package cn.tedu.egweb0.controller;

import cn.tedu.egweb0.base.config.response.JsonResult;
import cn.tedu.egweb0.base.config.response.StatusCode;
import cn.tedu.egweb0.mapper.UserMapper;
import cn.tedu.egweb0.pojo.dto.UserAddParam;
import cn.tedu.egweb0.pojo.entity.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@Slf4j
@RestController
@RequestMapping("/v1/user")
public class UserController {
    @Autowired
    private UserMapper userMapper;

    @PostMapping("/add")
    public JsonResult add(@RequestBody UserAddParam userAddParam) {
        log.debug("userAddParam:{}", userAddParam);

        User user = new User();

        BeanUtils.copyProperties(userAddParam, user);

        user.setCreateTime(new Date());

        int i = userMapper.insert(user);

        if (i > 0) {
            return JsonResult.ok();
        }else {
            return new JsonResult(StatusCode.OPERATION_FAILED);
        }

    }
}
